using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;

public class Player : MonoBehaviour
{	
	[Serializable]
	public class PlayerInfo
	{
		public string name;
		public ColorSerializable colorSerializable;
        public Color color { get { return colorSerializable.color; } set { colorSerializable = value; } }

		public PlayerInfo()
		{
			name = "Undefined";
			color = Color.grey;
		}
		public PlayerInfo(string name, Color color)
		{
			this.name = name;
			this.color = color;
		}
	}

	public int x, y, x1, y1;
	private GameObject first = null;
	private GameObject second = null;
	private GameObject temp = null;
	private int rows;
    private Camera gameCam;
	private IBoardDisplay iboardDisplay;
    private Board board;
	private int playerID;

    private GameObject bonus1;
    private GameObject bonus2;
    private GameObject bonus3;

	private Dictionary<int, PlayerInfo> m_players = new Dictionary<int, PlayerInfo>();

	void Start()
    {
        UICamera.genericEventHandler = this.gameObject;

       // bonus = GameObject.Find("Bonuses").GetComponent<Bonuses>();

       // bonus1 = GameObject.Find("Bonus1");
       // bonus2 = GameObject.Find("Bonus2");
       // bonus3 = GameObject.Find("Bonus3");
    }
	
    public void Init(GameManager gamemanager, int id)
    {
        board = gamemanager.mainBoard;

		playerID = id;
		
		iboardDisplay = gamemanager.mainBoardDisplay;
		
        rows = iboardDisplay.GetRowsCount();

        CreateGrid grid = new CreateGrid();

        grid.CreateGridColliders(rows, iboardDisplay, this);
    }
	
	public void UpdateTransformCollider()
	{
		GameObject[] objects;
		objects = GameObject.FindGameObjectsWithTag("collider");
		
		GameObject[,] obj = new GameObject[rows, rows];
		
		int count = 0;
		
		for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < rows; j++)
            {
				obj[j, i] = objects[count];
				
				count++;
            }
        }
		
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < rows; j++)
            {
				var collider = obj[j, i].GetComponent<BoxCollider>();
				collider.transform.localScale = new Vector3(collider.transform.localScale.x, collider.transform.localScale.y, 0.1f);
	
				Vector3 position = iboardDisplay.GetPosition(j, i);
				ScreenToWorld.PositionTransform (obj[j, i].transform, (int)position.x, (int)position.y);
				
				Rect size = iboardDisplay.GetSize(j, i);
				ScreenToWorld.ScaleCollider (obj[j, i], size);
            }
        }
	}

	public int AddPlayerInfo(PlayerInfo info)
	{
		for (int i = 0; i < int.MaxValue; i++)
		{
			if (!m_players.ContainsKey(i))
			{
				m_players.Add(i, info);
				return i;
			}
		}
		return -1;
	}

    public void SetPlayers(Dictionary<int, PlayerInfo> players)
    {
        m_players = players;
    }

	public PlayerInfo GetPlayerInfo(int id)
	{
		PlayerInfo info;
		m_players.TryGetValue(id, out info);
		return info;
	}

	public bool RemovePlayerInfo(int id)
	{
		return m_players.Remove(id);
	}
	
	void OnPress(bool isPressed)
	{
		if(isPressed)
		{
			first = UICamera.hoveredObject;
			
			if(first.tag == "collider")
			{		
				x = first.GetComponent<ColliderPosition>().GetX();
				y = first.GetComponent<ColliderPosition>().GetY();

                //bonus.GetPick(x, y);
			}
			else
				return;
		}
		else
		{
			temp = null;
		}
	}
	
	void OnDrag()
	{
		float distance;
		
		second = UICamera.hoveredObject;
		
		if(second != null & first != null)
		{
			distance = Vector2.Distance(first.transform.position, second.transform.position);
		
			if(second != temp & second != first & second.tag == "collider" & distance < 1)
			{
				x1 = second.GetComponent<ColliderPosition>().GetX();
				y1 = second.GetComponent<ColliderPosition>().GetY();
			
				board.MakeSwap(x, y, x1, y1, playerID);
			
				temp = second;
			
				first = null;
				second = null;

			}
		}
	}

    void OnClick()
    {
        GameObject clickObject = UICamera.currentTouch.current;

        if (clickObject == bonus1)
        {
            Debug.Log(clickObject);

            var particlePosition = new Vector3(0, 0, 0);
            var obj = GameObject.Instantiate(Resources.Load("ShockFlash"), particlePosition, gameObject.transform.rotation) as GameObject;
            obj.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);

            DestroyObject(obj, 1.5f);

           // bonus.Points4XOn();
        }

        if (clickObject == bonus2)
        {
            Debug.Log(clickObject);
            //bonus.Points4XOn();
        }

        if (clickObject == bonus3)
        {
            Debug.Log(clickObject);
            //bonus.Points4XOn();
        }
    }
}